﻿package com.shuqi.model.parse.parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.shuqi.common.Constant;
import com.shuqi.common.utils.CheckTxtCatalogTool;
import com.shuqi.common.utils.FileReaderUtil;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.Util;
import com.shuqi.database.dao.impl.TxtCatalogDao;
import com.shuqi.interfaces.OnAfterLoadPage;
import com.shuqi.model.bean.BookContentInfo;
import com.shuqi.model.parse.BookContentParserBase;

/**
 * 解析TXT文件app
 * 
 * @author Jianda.Yang
 * @date 2013-12-13 上午10:21:53
 * @version 1.0
 */
public class BookContentLocalAppTXTParser extends BookContentParserBase {
    /** 编码类型 */
    private String mFileEncoding;
    /** 当前要读取的字节索引 */
    private int mCurIndex;
    /** 当前章缓存 */
    private BookContentInfo mCurrInfo;
    /** 当前章文件名 */
    private String mCurrFilePath;
    private FileReaderUtil mReaderUtil;
    private int mFileLength = 0;
    private boolean isHasNext = true;
    private boolean isHasPre = false;

    private String tag = "BookContentLocalAppTXTParser";

    private List<BookContentInfo> mCataLogDataList = null;
    /** 是否要变更当前索引(当在当前章最后一页跳到下章第一页,在往回翻一页，然后在向后翻一页的情况) */
    public static boolean change = false;// 7.3.9


    /**
     * 
     * @param context
     * @param params BookContentInfo
     * @param byteIndex
     * @return
     */
    @Override
    public BookContentInfo getCurInfos(Context context, BookContentInfo params, boolean isNext,
            OnOneChpaterLoadDone callBack) {

        mCurrFilePath = params.getFliePath();
        if (!change) {
            mCurIndex = params.getStartIndex();
        }
        // Log4an.i("draw", "【getCurInfos】mCurIndex=" + mCurIndex);
        Log4an.i(tag, "接收mCurIndex=" + mCurIndex + ",mCurrFilePath=" + mCurrFilePath);
        if (TextUtils.isEmpty(mCurrFilePath) || !new File(mCurrFilePath).exists()) {
            params.setErrorMessage("文件已删除");
            return null;
        }
        try {
            if (mReaderUtil == null) {
                mReaderUtil = new FileReaderUtil();
                mReaderUtil.open(mCurrFilePath);
                mFileLength = (int) mReaderUtil.getTextLength();
                Log4an.d(tag, "mFileLength=" + mFileLength);
                mFileEncoding = Util.getTxtFileEncoding(context, mCurrFilePath);
                Log4an.d(tag, "mFileEncoding=" + mFileEncoding);
            }
            if (SharedPreferenceUtil.getFileCatalogTool(context, mCurrFilePath)) {
                // Log4an.i(tag, "1.mCataLogDataList is null=" + (mCataLogDataList == null));
                if (mCataLogDataList == null) {
                    // Log4an.e(tag, "**********AAAAAAAA***********");
                    mCataLogDataList =
                            TxtCatalogDao.getInstance().getTxtCatalogListByFilePath(mCurrFilePath);
                }
            } else {
                getBookCatalogList();
            }
            mCurrInfo = getInfo(context, params, mCurIndex, isNext);
            if (mCurrInfo != null) {
                mCurrInfo.setEncoding(mFileEncoding);
                mCurrInfo.setBookname(Util.getSubNameFromPath(mCurrFilePath, ".txt"));
                mCurrInfo.setTotalLength(mFileLength);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // Log4an.e(Log4an.TAG_YJD, "文件没找到");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Log4an.e(Log4an.TAG_YJD, "I/O异常");
        }
        return mCurrInfo;
    }

    private void logInfo(String strIndex, BookContentInfo info) {
        // Log4an.e(tag, "【" + strIndex + "】" + "preStart=" + info.getPrevChapter().getStartIndex()
        // + ",preEnd=" + info.getPrevChapter().getEndIndex() + ",curStart="
        // + info.getCurChapter().getStartIndex() + ",curEnd="
        // + info.getCurChapter().getEndIndex() + ",nextStart="
        // + info.getNextChapter().getStartIndex() + ",nextEnd="
        // + info.getNextChapter().getEndIndex());
    }

    private void logTempInfo(String strIndex, TempBookContentInfo info) {
        // Log4an.i(tag,
        // "【" + strIndex + "】start=" + info.getStartIndex() + ",end=" + info.getEndIndex());
    }

    class TempBookContentInfo {
        private int startIndex;
        private int endIndex;
        private String chapterName;

        public int getStartIndex() {
            return startIndex;
        }

        public void setStartIndex(int startIndex) {
            this.startIndex = startIndex;
        }

        public int getEndIndex() {
            return endIndex;
        }

        public void setEndIndex(int endIndex) {
            this.endIndex = endIndex;
        }

        /**
         * @return the chapterName
         */
        public String getChapterName() {
            return chapterName;
        }

        /**
         * @param chapterName the chapterName to set
         */
        public void setChapterName(String chapterName) {
            this.chapterName = chapterName;
        }

    }

    TempBookContentInfo curInfoToPre = new TempBookContentInfo();
    TempBookContentInfo curInfoToNext = new TempBookContentInfo();

    private boolean isFirst = true;

    /**
     * 获取内容
     * 
     * @param mCurIndex 当前索引
     * @param isNext true：下一段数据；false：上一段数据
     * @return
     */
    private BookContentInfo getInfo(Context context, BookContentInfo params, int mCurIndex,
            boolean isNext) {
        BookContentInfo info = params;
        if (mCurIndex < 0 && !isNext) {
            return info;
        } else if (mCurIndex >= mFileLength - 1 && isNext) {
            return info;
        } else {
            int index = mCurIndex;
            int readLen = 0;

            if (!change && info.getStartIndex() == 0 && info.getEndIndex() == 0) {// 从本地书籍初始加载或者从书签加载
                index = info.getBookmarkByteOffset();
                // Log4an.i(tag, "【书签】index=" + index);
            } else {
                isFirst = true;
            }
            // Log4an.i(tag, "1.index=" + index);
            TempBookContentInfo preInfo = null;
            TempBookContentInfo curInfo = null;
            TempBookContentInfo nextInfo = null;
            // Log4an.d(tag, "【getInfo】mCataLogDataList is null=" + (mCataLogDataList == null));

            if (isNext) {
                if (mCataLogDataList != null && mCataLogDataList.size() > 0) {
                    for (int i = 0; i < mCataLogDataList.size(); i++) {
                        int curStart = mCataLogDataList.get(i).getStartIndex();
                        int curEnd = mCataLogDataList.get(i).getEndIndex();
                        int curLen = curEnd - curStart;
                        int nextStart = 0, nextEnd = 0, nextLen = 0;
                        int preStart = 0, preEnd = 0, preLen = 0;
                        if (index >= curStart && index < curEnd) {

                            // Log4an.e(tag, "【next】章节索引：" + i + ",start=" + curStart + ",end="
                            // + curEnd + ",Len=" + curLen + ",index=" + index);

                            curInfo = new TempBookContentInfo();
                            preInfo = new TempBookContentInfo();
                            nextInfo = new TempBookContentInfo();

                            /*-----------设置当前数据索引------------*/
                            if (isFirst) {
                                curInfo.setStartIndex(curStart);
                                isFirst = false;
                            } else {
                                curInfo.setStartIndex(index);
                            }
                            if (curLen > Constant.READ_BYTEBUFFER_LENTH) {// 当前章长度大于20K
                                int tCurEnd = index + Constant.READ_BYTEBUFFER_LENTH;// 当前临时结束点
                                // Log4an.e(tag, "tCurEnd=" + tCurEnd);
                                if (tCurEnd > curEnd) {// 当前结束点超过本章长度
                                    curInfo.setEndIndex(curEnd);
                                } else {
                                    curInfo.setEndIndex(tCurEnd);
                                }
                            } else {// 当前章长度小于20K
                                curInfo.setEndIndex(curEnd);
                            }
                            curInfo.setChapterName(mCataLogDataList.get(i).getCurChapterName());
                            // Log4an.e("draw", "【1】chapterName=" + curInfo.getChapterName());
                            logTempInfo("1.curinfo", curInfo);

                            /*-----------设置上个数据索引------------*/
                            if (i != 0) {
                                if (i > 0) {
                                    preStart = mCataLogDataList.get(i - 1).getStartIndex();
                                    preEnd = mCataLogDataList.get(i - 1).getEndIndex();
                                    preLen = preEnd - preStart;
                                    preInfo.setEndIndex(curInfo.getStartIndex());

                                    if (curInfo.getStartIndex() == curStart) {// 判断当前页是否是章首
                                        if (preLen > Constant.READ_BYTEBUFFER_LENTH) {// 上一章大于20K
                                            // 取上一章的余数
                                            int tPreLen = preLen % Constant.READ_BYTEBUFFER_LENTH;
                                            if (tPreLen != 0) {// 余数不等于0的情况
                                                preInfo.setStartIndex(preEnd - tPreLen);
                                            } else {// (例如章节为80K)cx
                                                preInfo.setStartIndex(preEnd
                                                        - Constant.READ_BYTEBUFFER_LENTH);
                                            }
                                        } else {
                                            preInfo.setStartIndex(preStart);
                                        }
                                    } else {
                                        preInfo.setStartIndex(preInfo.getEndIndex()
                                                - Constant.READ_BYTEBUFFER_LENTH);
                                    }
                                }
                                preInfo.setChapterName(mCataLogDataList.get(i - 1)
                                        .getCurChapterName());
                                // Log4an.e("draw", "【2】chapterName=" + preInfo.getChapterName());
                            } else {
                                if (index != 0) {
                                    preInfo.setStartIndex(curInfoToPre.getStartIndex());
                                    preInfo.setEndIndex(curInfoToPre.getEndIndex());
                                } else {
                                    preInfo.setStartIndex(0);
                                    preInfo.setEndIndex(0);
                                }
                                preInfo.setChapterName(mCataLogDataList.get(i).getCurChapterName());
                                // Log4an.e("draw", "【2A】chapterName=" + preInfo.getChapterName());
                            }

                            logTempInfo("2.preInfo", preInfo);

                            /*-----------设置下个数据索引------------*/
                            if (i < mCataLogDataList.size() - 1) {// 不是最后一章
                                nextStart = mCataLogDataList.get(i + 1).getStartIndex();
                                nextEnd = mCataLogDataList.get(i + 1).getEndIndex();
                                nextLen = nextEnd - nextStart;
                                // Log4an.e(tag, "nextStart=" + nextStart + ",nextEnd=" + nextEnd
                                // + ",nextLen=" + nextLen);
                                nextInfo.setStartIndex(curInfo.getEndIndex());

                                if (nextInfo.getStartIndex() == curEnd) {// 当前章是否读完
                                    if (nextLen > Constant.READ_BYTEBUFFER_LENTH) {// 下一章长度大于20K
                                        int tNextEnd =
                                                nextInfo.getStartIndex()
                                                        + Constant.READ_BYTEBUFFER_LENTH;// 下一个临时结束点.
                                        // Log4an.e(tag, "tNextEnd=" + tNextEnd);
                                        if (tNextEnd > nextLen) {// 下个临时结束点超过了下一章长度
                                            nextInfo.setEndIndex(nextEnd);
                                        } else {
                                            nextInfo.setEndIndex(tNextEnd);
                                        }
                                    } else {
                                        nextInfo.setEndIndex(nextEnd);
                                    }
                                } else {
                                    int tNextEnd =
                                            nextInfo.getStartIndex()
                                                    + Constant.READ_BYTEBUFFER_LENTH;// 下一个临时结束点.
                                    // Log4an.e(tag, "tNextEnd=" + tNextEnd);
                                    if (tNextEnd > curEnd) {// 下个临时结束点超过了当前章长度
                                        nextInfo.setEndIndex(curEnd);
                                    } else {
                                        nextInfo.setEndIndex(tNextEnd);
                                    }
                                }
                                nextInfo.setChapterName(mCataLogDataList.get(i + 1)
                                        .getCurChapterName());
                                // Log4an.e("draw", "【3】chapterName=" + nextInfo.getChapterName());
                            } else {// 是最后一章
                                nextInfo.setStartIndex(0);
                                nextInfo.setEndIndex(0);
                                nextInfo.setChapterName(mCataLogDataList.get(i).getCurChapterName());
                                // Log4an.e("draw", "【3A】chapterName=" + nextInfo.getChapterName());
                            }
                            logTempInfo("3.nextInfo", nextInfo);
                            curInfoToPre = curInfo;
                            curInfoToNext = curInfo;
                            info.getPrevChapter().setStartIndex(preInfo.getStartIndex());
                            info.getPrevChapter().setEndIndex(preInfo.getEndIndex());
                            info.getPrevChapter().setChapterName(preInfo.getChapterName());

                            info.getCurChapter().setStartIndex(curInfo.getStartIndex());
                            info.getCurChapter().setEndIndex(curInfo.getEndIndex());
                            info.getCurChapter().setChapterName(curInfo.getChapterName());

                            info.getNextChapter().setStartIndex(nextInfo.getStartIndex());
                            info.getNextChapter().setEndIndex(nextInfo.getEndIndex());
                            info.getNextChapter().setChapterName(nextInfo.getChapterName());

                            logInfo("【next】BookContentInfo", info);
                            break;
                        }
                    }
                }
                // 查过查找到章节信息时，从章节起始索引开始读，否则按照20k的逻辑读
                // Log4an.i(tag, "[往后翻页]curInfo is null=" + (curInfo == null));
                if (curInfo != null) {
                    index = info.getStartIndex();
                } else {
                    index =
                            (index / Constant.READ_BYTEBUFFER_LENTH)
                                    * Constant.READ_BYTEBUFFER_LENTH;
                }
                // Log4an.i(tag, "A【index】index=" + index);
                int tempReadLen =
                        info.getEndIndex() > 0 ? info.getEndIndex()
                                : (index + Constant.READ_BYTEBUFFER_LENTH);

                // Log4an.i(tag, "A【tempReadLen】tempReadLen=" + tempReadLen);
                if (index >= mFileLength) {
                    index = 0;
                    readLen = mFileLength;
                } else if ((tempReadLen) >= mFileLength) {
                    // if ((mFileLength - index < 10)) {
                    // readLen = mFileLength;
                    // isHasNext = false;
                    // } else {
                    readLen = mFileLength - index;
                    isHasNext = false;
                    // }
                } else {
                    if (tempReadLen < mFileLength && mFileLength - tempReadLen < 10) {
                        readLen = (tempReadLen - index) + (mFileLength - tempReadLen);
                        isHasNext = false;
                    } else {
                        readLen = (tempReadLen - index);
                    }
                }
                // Log4an.i(tag, "【向后翻页】readLen=" + readLen);
            } else {
                if (mCataLogDataList != null && mCataLogDataList.size() > 0) {
                    for (int i = 0; i < mCataLogDataList.size(); i++) {
                        int curStart = mCataLogDataList.get(i).getStartIndex();
                        int curEnd = mCataLogDataList.get(i).getEndIndex();
                        int curLen = curEnd - curStart;
                        int preStart = 0, preEnd = 0, preLen = 0;
                        if (index >= curStart && index < curEnd) {

                            // Log4an.e(tag, "【pre】章节索引：" + i + ",start=" + curStart + ",end="
                            // + curEnd + ",Len=" + curLen + ",index=" + index);

                            curInfo = new TempBookContentInfo();
                            preInfo = new TempBookContentInfo();
                            nextInfo = new TempBookContentInfo();

                            /*-----------设置当前数据索引------------*/
                            curInfo.setStartIndex(curStart);
                            if (curLen > Constant.READ_BYTEBUFFER_LENTH) {// 当前章长度大于20K
                                int tCurEnd = index + Constant.READ_BYTEBUFFER_LENTH;// 当前临时结束点
                                // Log4an.e(tag, "tCurEnd=" + tCurEnd);
                                if (tCurEnd > curEnd) {// 当前结束点超过本章长度
                                    curInfo.setEndIndex(curEnd);
                                } else {
                                    curInfo.setEndIndex(tCurEnd);
                                }
                            } else {// 当前章长度小于20K
                                curInfo.setEndIndex(curEnd);
                            }
                            curInfo.setChapterName(mCataLogDataList.get(i).getCurChapterName());
                            // Log4an.e("draw", "【1】chapterName=" + curInfo.getChapterName());
                            logTempInfo("1.curinfo", curInfo);

                            /*-----------设置下个数据索引------------*/
                            if (index != 0) {
                                nextInfo.setStartIndex(curInfoToNext.getStartIndex());
                                nextInfo.setEndIndex(curInfoToNext.getEndIndex());
                                nextInfo.setChapterName(curInfoToNext.getChapterName());
                                // Log4an.e("draw", "【2】chapterName=" + nextInfo.getChapterName());
                            } else {
                                // nextInfo.setStartIndex(0);
                                // nextInfo.setEndIndex(0);
                            }
                            logTempInfo("2.nextInfo", nextInfo);

                            /*-----------设置上个数据索引------------*/
                            if (i > 0) {
                                preStart = mCataLogDataList.get(i - 1).getStartIndex();
                                preEnd = mCataLogDataList.get(i - 1).getEndIndex();
                                preLen = preEnd - preStart;
                                preInfo.setChapterName(mCataLogDataList.get(i - 1)
                                        .getCurChapterName());
                                // Log4an.e("draw", "【3】chapterName=" + preInfo.getChapterName());
                            } else {
                                preStart = mCataLogDataList.get(i).getStartIndex();
                                preEnd = mCataLogDataList.get(i).getEndIndex();
                                preLen = preEnd - preStart;
                                preInfo.setChapterName(mCataLogDataList.get(i).getCurChapterName());
                                // Log4an.e("draw", "【3】chapterName=" + preInfo.getChapterName());
                            }
                            preInfo.setEndIndex(curInfo.getStartIndex());

                            if (curInfo.getStartIndex() == curStart) {// 判断当前页是否是章首
                                if (preLen > Constant.READ_BYTEBUFFER_LENTH) {// 上一章大于20K
                                    // 取上一章的余数
                                    int tPreLen = preLen % Constant.READ_BYTEBUFFER_LENTH;
                                    if (tPreLen != 0) {// 余数不等于0的情况
                                        preInfo.setStartIndex(preEnd - tPreLen);
                                    } else {// (例如章节为80K)cx
                                        preInfo.setStartIndex(preEnd
                                                - Constant.READ_BYTEBUFFER_LENTH);
                                    }
                                } else {
                                    preInfo.setStartIndex(preStart);
                                }
                            } else {
                                preInfo.setStartIndex(preInfo.getEndIndex()
                                        - Constant.READ_BYTEBUFFER_LENTH);
                            }

                            logTempInfo("2.preInfo", preInfo);
                            curInfoToNext = curInfo;
                            info.getPrevChapter().setStartIndex(preInfo.getStartIndex());
                            info.getPrevChapter().setEndIndex(preInfo.getEndIndex());
                            info.getPrevChapter().setChapterName(preInfo.getChapterName());

                            info.getCurChapter().setStartIndex(curInfo.getStartIndex());
                            info.getCurChapter().setEndIndex(curInfo.getEndIndex());
                            info.getCurChapter().setChapterName(curInfo.getChapterName());

                            info.getNextChapter().setStartIndex(nextInfo.getStartIndex());
                            info.getNextChapter().setEndIndex(nextInfo.getEndIndex());
                            info.getNextChapter().setChapterName(nextInfo.getChapterName());
                            logInfo("【pre】BookContentInfo", info);
                            break;
                        }
                    }
                }
                readLen =
                        Math.min(Constant.READ_BYTEBUFFER_LENTH,
                                info.getEndIndex() - info.getStartIndex() + 1);
            }
            Log4an.i(tag, "【BookContentLocalAppTXTParser】readLen=" + readLen + ",index=" + index
                    + ",mFileLength=" + mFileLength);
            if (index + readLen >= mFileLength) {
                readLen = mFileLength - index;
            }
            MappedByteBuffer mByteBuffer = mReaderUtil.getMappedByteBuffer(index, readLen);
            byte[] readByteArray = new byte[readLen];
            mByteBuffer.get(readByteArray, 0, readByteArray.length);
            info.setCurChapterBytes(readByteArray);
            info.setEncoding(mFileEncoding);
//            Log4an.e(tag, "【判断对象】" + "preInfo is null=" + (preInfo == null) + ",curInfo is null="
//                    + (curInfo == null) + ",nextInfo is null=" + (nextInfo == null));
            if (curInfo == null && preInfo == null && nextInfo == null) {
                info.setStartIndex(index);
                info.setEndIndex(info.getStartIndex() + readLen);
                info.getPrevChapter().setStartIndex(
                        Math.max(index - Constant.READ_BYTEBUFFER_LENTH, 0));
                info.getPrevChapter().setEndIndex(Math.max(index - 1, 0));
                info.getNextChapter().setStartIndex(info.getEndIndex() + 1);
                info.getNextChapter().setEndIndex(
                        Math.min(info.getNextChapter().getStartIndex()
                                + Constant.READ_BYTEBUFFER_LENTH - 1, mFileLength - 1));
            }

//            Log4an.e(tag, "【" + 0 + "】" + "preStart=" + info.getPrevChapter().getStartIndex()
//                    + ",preEnd=" + info.getPrevChapter().getEndIndex() + ",curStart="
//                    + info.getCurChapter().getStartIndex() + ",curEnd="
//                    + info.getCurChapter().getEndIndex() + ",nextStart="
//                    + info.getNextChapter().getStartIndex() + ",nextEnd="
//                    + info.getNextChapter().getEndIndex());
            curInfo = null;
            preInfo = null;
            nextInfo = null;
        }

        return info;
    }

    @Override
    public BookContentInfo getCurrInfo(Activity context) {
        return mCurrInfo;
    }

    @Override
    public void loadPrevInfo(Activity context, final OnAfterLoadPage onAfterLoadPage,
            BookContentInfo params) {
        synchronized (BookContentLocalAppTXTParser.this) {
            final BookContentInfo info = getCurInfos(context, params, false);
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onAfterLoadPage.runOnUiThread(info);
                }
            });
        }
    }

    @Override
    public void loadNextInfo(Activity context, final OnAfterLoadPage onAfterLoadPage,
            BookContentInfo params) {
        synchronized (BookContentLocalAppTXTParser.this) {
            final BookContentInfo info = getCurInfos(context, params, true);
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onAfterLoadPage.runOnUiThread(info);
                }
            });
        }
    }

    /**
     * 用于当前章节最后一页和下章第一页反复切换的情况
     * 
     * @param index
     * @return
     */
    private int changeChapterIndex(int index) {
        if (mCataLogDataList != null && mCataLogDataList.size() > 0) {
            for (int i = 0; i < mCataLogDataList.size(); i++) {
                if (index >= mCataLogDataList.get(i).getStartIndex()
                        && index < mCataLogDataList.get(i).getEndIndex()) {
                    index = mCataLogDataList.get(i).getEndIndex();
                    break;
                }
            }
        }
        return index;
    }

    @Override
    public boolean hasPreChapter(int index) {
        // Log4an.i("draw", "【TXT解析】index=" + index);
        if (index > 0) {
            isHasPre = true;
        } else {
            isHasPre = false;
        }
        return isHasPre;
    }

    @Override
    public boolean hasNextChapter(int index) {
        // Log4an.i("draw", "【txt】change=" + change + ",index=" + index);
        if (change) {
            // Log4an.i("draw", "【txt】index=" + index);
            mCurIndex = changeChapterIndex(index);
            // Log4an.i("draw", "【获取】mCurIndex=" + mCurIndex);
        }
        if (index < mFileLength) {
            if ((index + Constant.READ_BYTEBUFFER_LENTH) >= mFileLength) {
                if ((mFileLength - index < 10)) {
                    isHasNext = false;
                } else {
                    isHasNext = true;
                }
            } else {
                int temp = index + Constant.READ_BYTEBUFFER_LENTH;
                if (temp < mFileLength && mFileLength - temp < 10) {
                    isHasNext = false;
                } else {
                    isHasNext = true;
                }
            }
        } else {
            isHasNext = false;
        }

        return isHasNext;
    }

    @Override
    public List<BookContentInfo> getBookCatalogList() {
        mCataLogDataList = CheckTxtCatalogTool.getInstance().getmRunCatalogList();
        return mCataLogDataList;
    }

    @Override
    public boolean isPageFetching() {
        return false;
    }

    /** 根据当前进度获取章节名 */
    public BookContentInfo getCurCharpterNameFromProgress(Context context, BookContentInfo params) {
        BookContentInfo curInfo = params;

        mCurrFilePath = params.getFliePath();
        mCurIndex = params.getStartIndex();
        if (TextUtils.isEmpty(mCurrFilePath) || !new File(mCurrFilePath).exists()) {
            params.setErrorMessage("文件已删除");
            return null;
        }

        try {
            if (mReaderUtil == null) {
                mReaderUtil = new FileReaderUtil();
                mReaderUtil.open(mCurrFilePath);
                mFileLength = (int) mReaderUtil.getTextLength();
                mFileEncoding = Util.getTxtFileEncoding(context, mCurrFilePath);
            }
            if (SharedPreferenceUtil.getFileCatalogTool(context, mCurrFilePath)) {// 断章完成
                if (mCataLogDataList == null) {
                    mCataLogDataList =
                            TxtCatalogDao.getInstance().getTxtCatalogListByFilePath(mCurrFilePath);
                }

                if (mCurIndex < 0 || mCurIndex > mFileLength - 1) {// 索引不再范围内
                    return curInfo;
                }

                if (mCataLogDataList == null || mCataLogDataList.size() <= 0) {
                    return curInfo;
                }
                mCataLogDataList.get(mCataLogDataList.size() - 1).setEndIndex(mFileLength);
                for (int i = 0; i < mCataLogDataList.size(); i++) {
                    if (mCataLogDataList.get(i).getStartIndex() <= mCurIndex
                            && mCurIndex < mCataLogDataList.get(i).getEndIndex()) {
                        curInfo = mCataLogDataList.get(i);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // Log4an.e(Log4an.TAG_YJD, "文件没找到");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Log4an.e(Log4an.TAG_YJD, "I/O异常");
        }

        return curInfo;
    }
}
